package com.hnu.highlynewapp.app;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnu.highlynewapp.entity.PolicyEntity;
import com.hnu.highlynewapp.entity.ServiceProductEntity;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.utils.ValidationUtils;
import com.hnu.highlynewapp.vo.PublishPolicyAnalyseVo;
import com.hnu.highlynewapp.vo.PublishPolicyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hnu.highlynewapp.entity.PolicyAnalyseEntity;
import com.hnu.highlynewapp.service.PolicyAnalyseService;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 政策解读
 *
 * @author hnu_worker
 * @email 825963704@qq.com
 * @date 2021-01-03 10:35:56
 */
@Api(tags = "政策api管理")
@RestController
@RequestMapping("/policyanalyse")
public class PolicyAnalyseController {
    @Autowired
    private PolicyAnalyseService policyAnalyseService;

    /**
     * 发布政策解读
     */
    @ApiOperation("发布政策解读")
    @PostMapping("/publish")
    public R publish(@Validated @RequestBody PublishPolicyAnalyseVo vo, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = ValidationUtils.getErrors(result);
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data",map);
        } else {
            PolicyAnalyseEntity policyEntity = new PolicyAnalyseEntity();
            BeanUtils.copyProperties(vo,policyEntity);
            policyEntity.setPublishTime(new Date());
            policyAnalyseService.save(policyEntity);
            return R.ok();
        }
    }

    /**
     * 获取所有政策解读（时间降序）
     * @return
     */
    @ApiOperation("根据检索条件获取所有政策解读")
    @PostMapping("/getlist")
    public R getList(@ApiParam(value = "政策分类") @RequestParam(value = "category",required = false) String category) {
        List<PolicyAnalyseEntity> list = policyAnalyseService.listByCondition(category);
        return R.ok().put("data", list);
    }

    /**
     * 根据id获取政策解读
     * @return
     */
    @ApiOperation("根据id获取政策解读")
    @GetMapping("/get/{policyAnalyseId}")
    public R getById(@PathVariable("policyAnalyseId") Long policyAnalyseId) {
        PolicyAnalyseEntity policyAnalyse = policyAnalyseService.getById(policyAnalyseId);
        if (policyAnalyse == null) {
            return R.error(BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getCode(), BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getMessage());
        }
        return R.ok().put("data", policyAnalyse);
    }

    /**
     * 根据角色id获取自己发布的政策解读
     * @return
     */
    @ApiOperation("根据角色id获取自己发布的政策解读")
    @GetMapping("/getlist/{role_id}")
    public R getListByRoleId(@ApiParam(value = "角色id",required = true) @PathVariable("role_id") Long role_id) {
        List<PolicyAnalyseEntity> list = policyAnalyseService.list(new QueryWrapper<PolicyAnalyseEntity>().eq("department_id", role_id).orderByDesc("publish_time"));
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @ApiIgnore
    @RequestMapping("/list")
    //@RequiresPermissions("highlynewapp:policyanalyse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = policyAnalyseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiIgnore
    @RequestMapping("/info/{policyAnalyseId}")
    //@RequiresPermissions("highlynewapp:policyanalyse:info")
    public R info(@PathVariable("policyAnalyseId") Long policyAnalyseId){
		PolicyAnalyseEntity policyAnalyse = policyAnalyseService.getById(policyAnalyseId);

        return R.ok().put("policyAnalyse", policyAnalyse);
    }

    /**
     * 保存
     */
    @ApiIgnore
    @RequestMapping("/save")
    //@RequiresPermissions("highlynewapp:policyanalyse:save")
    public R save(@RequestBody PolicyAnalyseEntity policyAnalyse){
		policyAnalyseService.save(policyAnalyse);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @RequestMapping("/update")
    //@RequiresPermissions("highlynewapp:policyanalyse:update")
    public R update(@RequestBody PolicyAnalyseEntity policyAnalyse){
		policyAnalyseService.updateById(policyAnalyse);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @RequestMapping("/delete")
    //@RequiresPermissions("highlynewapp:policyanalyse:delete")
    public R delete(@RequestBody Long[] policyAnalyseIds){
		policyAnalyseService.removeByIds(Arrays.asList(policyAnalyseIds));

        return R.ok();
    }

}
